import ykAuthpup from "@/components/yk-authpup/yk-authpup";
import homeApi from "@/static/js/enterpriseServer/index.js"
import demandApi from "@/static/js/enterpriseServer/demand.js"
import choseRegion from '@/components/chose-region/chose-region.vue'
import choseCate from "@/components/chose-cate/chose-cate.vue"
import { completeTask } from '@/static/js/points.js';
import wfAddress from "@/components/WF-address/WF-address.vue";
export default {
	components: {
		choseRegion,
		choseCate, ykAuthpup,wfAddress
	},
	data() {
		return {
			date: '',
			imgList: [],
			categoryName: '请选择',
			cateFirstId: '',
			cateSecondId: '',
			cateThirdId: '',
			demandDate: '请选择',
			cityName: '请选择',
			subCityName: '',
			cityCode: '',
			cateList: [],
			cateIndex: [0, 0, 0],
			demandPrice: '',
			demandDes: '',
			startDate: '',
			priceStatus: false,
			showChooseImg: false, //是否显示选择图片
			showChooseImgList: [{
				name: '拍摄',
				type: 1,
			}, {
				name: '从相册选择',
				type: 2,
			}], //选择图片方式
			activeImgType: -1, //选择图片方式
			permissionID: '', //权限id
			userInfo: {},
			showPointsTips: false,
			pointsObj: {},
			showCityList: false, //服务区域显示弹窗
      choseRegion1: null, //选择的服务地址
		};
	},
	onLoad() {
		this.getAllCate()
		this.startDate = this.$moment().add(1, 'days').format('YYYY-MM-DD')
		//获取当前时间
    this.demandDate = this.$moment().add(3, "days").format("YYYY-MM-DD");
	},
	methods: {
		//获取全部需求类型
		getAllCate() {
			homeApi.getServiceCate().then(res => {
				if (res.code === 1) {
					this.cateList = res.data
					console.log(this.cateList)
				} else {
					this.$tools.msg(res.msg)
				}
			})
		},

		//价格面议的切换
		changePrice() {
			this.priceStatus = !this.priceStatus
		},

		//图片上传
		ChooseImage() {
			let type = this.activeImgType
			uni.chooseImage({
				count: 5, //默认9
				sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: type == 1 ? ['camera'] : ['album'], //从相册选择
				success: (res) => {
					if (res.tempFilePaths.length > 0) {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							this.imgUpload(res.tempFilePaths[i]);
							uni.showLoading({
								title: '上传中'
							});
						}
					}
				}
			});
		},
		//获取OSS图片数据
		imgUpload(img) {
			homeApi.uploadImg(img).then(res => {
				let res1 = JSON.parse(res)
				if (res1.code === 1) {
					this.imgList.push(res1.data.fullurl)
					uni.hideLoading();
				} else {
					this.$tools.msg(res1.msg);
				}
			})
		},
		//展示图片
		ViewImage(e) {
			uni.previewImage({
				urls: this.imgList,
				current: e.currentTarget.dataset.url
			});
		},
		//删除图片
		DelImg(e) {
			uni.showModal({
				title: '温馨提示',
				content: '确定要删除此图片？',
				cancelText: '再想想',
				confirmText: '确定',
				success: res => {
					if (res.confirm) {
						this.imgList.splice(e.currentTarget.dataset.index, 1)
					}
				}
			})
		},

		//需求类型选择
		cateName(e) {
			console.log(e)
			this.categoryName = e[1].name;
      this.cateFirstId = e[0].id;
      this.cateSecondId = e[1].id;
		},

		//地址选择
		regionName(e) {
			console.log(e)
			this.cityName = e[0].name + '/' + e[1].name
			this.subCityName = e[1].name
			this.cityCode = e[1].id
		},

		//截止日期选择
		DateChange(e) {
			this.demandDate = e.detail.value
		},

		//发布任务需求
		async subDemand() {
			if (!this.cateFirstId) {
				this.$tools.msg('请选择需求类型')
				return
			}
			if (!this.demandDes) {
				this.$tools.msg('请填写需求描述')
				return
			}
			if (!this.demandDate) {
				this.$tools.msg('请选择截止时间')
				return
			}
			if (!this.cityCode) {
				this.$tools.msg('请选择服务地址')
				return
			}
			let obj = {
				one_cate_id: this.cateFirstId,
				two_cate_id: this.cateSecondId,
				third_cate_id: this.cateThirdId,
				describe: this.demandDes,
				money: this.priceStatus ? 0 : this.demandPrice,
				end_time: this.$moment(this.demandDate).valueOf() / 1000,
				city_code: this.cityCode,
				city_name: this.subCityName,
				annex: this.imgList.join(',')
			}
			console.log(obj)
			let res = await demandApi.addDemand(obj)
			if (res.code === 1) {
				this.imgList = []
				this.cityName = '请选择'
				this.demandDate = '请选择'
				this.categoryName = '请选择'
				this.demandDes = ''
				this.demandPrice = ''
				this.priceStatus = false
				this.$tools.msg('发布成功')
				this.completeQuest('C3')
				setTimeout(() => {
					uni.navigateTo({
						url: '/enterpriseServer/demandHall/index'
					})
				}, 2000)
			} else {
				this.$tools.msg(res.msg)
			}
		},

		//显示图片方式弹窗
		chooseImgType() {
			this.showChooseImg = true
		},

		//选择图片方式
		selectClick(e) {
			this.activeImgType = e.type
			if (e.type == 1) {
				this.openAuth('CAMERA')
			} else if (e.type == 2) {
				this.openAuth('WRITE_EXTERNAL_STORAGE')
			}
		},

		//打开授权弹窗
		openAuth(permissionID) {
			let sysTemInfo = uni.getSystemInfoSync().brand; //获取手机品牌
			console.log(sysTemInfo, 'sysTemInfo');
			if (sysTemInfo == 'huawei') {
				this.permissionID =
					permissionID; //这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
				setTimeout(() => {
					this.$refs['authpup'].open();
				}, 500)
			} else {
				this.ChooseImage()
			}
		},

		//用户授权权限后的回调
		changeAuth() {
			console.log('授权成功');
			//这里是权限通过后执行自己的代码逻辑
			this.ChooseImage()
		},

		//完成任务
		completeQuest(code) {
			completeTask({ code }).then(res => {
				if (res.code == 1) {
					this.showPointsTips = true;
					this.pointsObj = res.data;
				}
			})
			setTimeout(() => {
				this.showPointsTips = false;
			}, 3000);
		},


		showCityListFn() {
      this.showCityList = true;
    },

    closeCityList() {
      this.showCityList = false;
      this.choseRegion1 = null;
      this.cityCode = "";
      this.cityName = "请选择";
    },

    getCityInfo(data) {
      this.choseRegion1 = data;
      this.cityCode = data.id;
      this.cityName = data.name;
      this.showCityList = false;
    },
	}
}